<?php

namespace App\Validators\Personnel;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class StationValidator extends LaravelValidator{

    use PassesValidator;

    protected $rulesMessages=[
        'name.required' => '岗位名称是必填的',
        'name.unique' => '该岗位名称已经存在',
        'id.not_exists' => '还有用户在岗位中任职,不能被删除',
        'station_class_id.required' => '岗位分类是必选的'];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'name'=>'required|unique:station',
            'station_class_id'=>'required'
        ],
        ValidatorInterface::RULE_UPDATE => [
            'name'=>'required|unique:station_class,name,{id}',
            'station_class_id'=>'required'
        ],
        "DELETE" => [
            'id' =>'not_exists:user_station,station_id',
        ]
    ];

    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = array('{id}'=>Input::get('id'));
        foreach ($rules as $key=>$value){
            if(is_string($value) && str_contains($value,'{id}')){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
